var express = require('express');
var router = express.Router();
var Hotel = require('../../models/hotel.js');
var siteconfig = require('../../models/config/siteconfig.js');
var chkLogin = require("./filter/rules.js");


//筛选
router.get("/hotel/list",chkLogin);
router.get('/hotel/list', function(req, res, next) {
    var _pagesize=null,
        _page  = null;
    if(req.query.page){
        _page = req.query.page;
    }

    Hotel.getTen(_pagesize,_page,function(err,hotels,totalPage){
        if(err){
            hotels = [];
        }
        res.render('admin/hotel/list',{
            hotels: hotels,
            totalPage:totalPage,
            user: req.session.user
        });
    });
});
router.post("/hotel/list",chkLogin);
router.post('/hotel/search',function(req,res,next){
    var _CDbcriteria,
        _page = null;
    if(req.body.name || req.body.countries_id){
        _CDbcriteria = {
            name: req.body.name,
            country_id:req.body.countries_id
        }
    }

    Hotel.schTen(null,_page,_CDbcriteria,function(err,hotels,totalPage){
        if(err){
            hotels = [];
        }
        res.render('admin/hotel/search',{
            hotels: hotels,
            totalPage:totalPage,
            user: req.session.user
        });
    });
})

//添加
router.get("/hotel/add",chkLogin);
router.get('/hotel/add', function(req, res, next) {
    res.render('admin/hotel/add',{
        user: req.session.user
    });
});
//图片
router.post("/hotel/update",chkLogin);
router.post('/hotel/update', function(req, res, next) {
    var _id = req.body.id;
    if(_id){
        var _hotel={};
        _hotel.hotel_id =_id;
        _hotel.photo1 =req.files.photo1.path;
        _hotel.photo2 =req.files.photo2.path;
        _hotel.photo3 =req.files.photo3.path;
        _hotel.photo4 =req.files.photo4.path;
        _hotel.photo5 =req.files.photo5.path;
        var obj =new Hotel(_hotel);
        var _json ={};
        obj.update(function(err,hotels) {
            if (err) {
                _json.msg = "更新失败";
            }else{
                _json["succeed"] = true;
                _json.msg = "更新成功";
                _json.insertId = hotels.insertId;
            }
            res.send(_json);
        });
    }
});
router.post("/hotel/add",chkLogin);
router.post('/hotel/add', function(req, res, next) {
    var _json= {"success":false};
    var msgs = [];
    var result = {state:1,message:[].join("\\n"),data:{}}
    var check =  function(obj){
        for(var i in obj){
            msgs.push(obj[i]);
        }
    };
    var _hotel={};
    for(var i in req.body){
        //if((/^txt_/).test(i)){ }
        _hotel[i] =req.body[i];
    }
    //第一步上传信息，使用默认图片
    _hotel.photo1=siteconfig.images,
        _hotel.photo2=siteconfig.images,
        _hotel.photo3=siteconfig.images,
        _hotel.photo4=siteconfig.images,
        _hotel.photo5=siteconfig.images;
    //提交有错，将错误信息返回
    //if(msgs.length>0){
    //    res.send(_json);
    //    return;
    //}
    var obj =new Hotel(_hotel);
    obj.save(function(err,hotels) {
        if (err) {
            _json.msg = "更新失败";
        }else{
            _json["succeed"] = true;
            _json.msg = "更新成功";
            _json.insertId = hotels.insertId;
        }
        res.send(_json);
    });
});
//事务-------------------------------------
// 数据层 DbUtil.
//var con = DbUtil.openConnect();
//var trans = con.beginTrans();
//try{
//    con.save();
//// insert 1
//// insert 1
//// insert 1
//// insert 1
//    trans.commit();
//}catch(ex){
//    log.error(ex);
//    trans.roollBack();
//}finally{
//    con.close();
//    trans = null;
//    con = null;
//}
//-------------------------------------

//编辑
router.get("/hotel/edit",chkLogin);
router.get("/hotel/edit",function(req,res,next){
    var obj = new Hotel();
    obj.getOne(req.query.id,function(err,hotels){
        if(err){
            console.log("查询出错");
        }
        res.render('admin/hotel/edit',{
            hotels: hotels[0],
            user: req.session.user
        });
    });
});
router.post("/hotel/edit",chkLogin);
router.post("/hotel/edit",function(req,res,next){
    var _id = req.body.hotel_id;
    if(_id){
        var _hotel={};
        for(var i in req.body){
            //if((/^txt_/).test(i)){ }
            _hotel[i] =req.body[i];
        }
        var obj =new Hotel(_hotel);
        var _json ={};
        obj.update(function(err,hotels) {
            if (err) {
                _json.msg = "更新失败";
                console.log("error:::"+"更新失败");
            }else{
                _json["succeed"] = true;
                _json.msg = "更新成功";
                _json.insertId = hotels.insertId;
            }
            res.send(_json);
        });
    }
});

//删除
router.get("/hotel/delete",chkLogin);
router.get("/hotel/delete",function(req,res,next){
    var obj = new Hotel();
    obj.delete(req.query.id,function(err,hotels){
        if(err){
            console.log("删除出错");
            req.writeHead(302, {
                'Location': '/404.html'
                //add other headers here...
            });
            req.end();
        }
        res.redirect('/admin/hotel/list');
    });
});

module.exports = router;